package com.zhoug.mpchart.data;

import com.zhoug.mpchart.utils.MPChartUtils;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;

import androidx.annotation.Keep;

/**
 * 图表数据封装
 *
 * @Author 35574
 * @Date 2020/12/31
 * @Description
 */
@Keep
public class HkChartData {
    private String Name;//名称(x轴)
    private LinkedHashMap<String, Float> Data;//数据（y轴）
    private String Desc;
    private Object Tag;

    public HkChartData() {
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public Object getTag() {
        return Tag;
    }

    public void setTag(Object tag) {
        this.Tag = tag;
    }

    public LinkedHashMap<String, Float> getData() {
        return Data;
    }

    public void setData(LinkedHashMap<String, Float> data) {
        Data = data;
    }

    public HkChartData put(String key, float value) {
        if (Data == null) {
            synchronized (this) {
                if (Data == null) {
                    Data = new LinkedHashMap<>();
                }
            }
        }
        Data.put(key, value);
        return this;
    }


    public String[] getKeys() {
        if (Data != null && Data.size() > 0) {
            String[] strings = new String[Data.size()];
            return Data.keySet().toArray(strings);
        }
        return null;
    }

    public float getValue(String key) {
        if (Data != null && Data.size() > 0) {
            Float value = Data.get(key);
            return value != null ? value : 0;
        }
        return 0;
    }

    public String getDesc() {
        return Desc;
    }

    public void setDesc(String desc) {
        Desc = desc;
    }

    /**
     * 获取Value中所有数据的和
     *
     * @return
     */
    public float getDataAllValue() {
        if (Data != null && Data.size() > 0) {
            float allValue = 0;
            for (String s : Data.keySet()) {
                Float value = Data.get(s);
                if (value != null) {
                    allValue = allValue + value;
                }
            }
            return allValue;
        }
        return 0;
    }


    public static String[] getKeys(List<HkChartData> dataList) {
        if (dataList != null && dataList.size() > 0) {
            String[] keys ;
            for (HkChartData item : dataList) {
                if (item != null) {
                    keys = item.getKeys();
                    if (keys != null && keys.length > 0) {
                        return keys;
                    }
                }
            }
        }
        return null;
    }




    /**
     * 按名字排序
     */
    public static Comparator<HkChartData> comparatorByName = (o1, o2) -> {
        if (o1 != null && o2 != null) {
            return MPChartUtils.compareTo(o1.getName(), o2.getName());
        }
        return 0;
    };

    /**
     * 按KEY排序
     */
    public static class ComparatorByKeyValue implements Comparator<HkChartData> {
        private String key;
        private boolean desc = false;

        public ComparatorByKeyValue(String key, boolean desc) {
            this.key = key;
            this.desc = desc;
        }

        @Override
        public int compare(HkChartData o1, HkChartData o2) {
            float a = o1.getValue(key) - o2.getValue(key);
            int i = a > 0 ? 1 : a < 0 ? -1 : 0;
            return desc ? -i : i;
        }
    }

}

